<?php

require_once("./common.php");
require_once("./utils.php");

class SoldierCron{

	function draftSoldier($param)
	{
		
		$d_rows=sql_fetch_rows("SELECT * FROM mem_city_draft WHERE state_endtime < unix_timestamp()");
        //选择所有已完成的征兵
		foreach ($d_rows as $row){
		  sql_query("Delete from sys_city_draftqueue where id=$row[id]");
          sql_query("DELETE FROM mem_city_draft WHERE id=$row[id]");
          $id = sql_fetch_one_cell("select id from sys_city_draftqueue where cid='$row[cid]' and xy='$row[xy]' order by queuetime limit 1");	
          if (!empty($id))
		  {  //如果存在下一个队列，则载入下个队列                                                                     
		      sql_query("update sys_city_draftqueue set state=1,state_starttime=unix_timestamp() where id='$id'"); 
		      sql_query("insert into mem_city_draft (select id,cid,xy,sid,count,state_starttime+needtime from sys_city_draftqueue where id='$id')");
		  } 
  
                
		  //更新城内兵的数量
            //addCitySoldier($row[cid],$row[sid],$row[count]);
            $s_result=sql_fetch_one_cell("SELECT count FROM sys_city_soldier WHERE sid=$row[sid] AND cid=$row[cid]");
            if (!empty($s_result)){
                $s_count=$row[count]+$s_result;
                sql_query("update sys_city_soldier set count =$s_count where sid = $row[sid] and cid=$row[cid]");
            }
            else{
                sql_query("Insert into sys_city_soldier (cid,sid,count) values ($row[cid],$row[sid],$row[count])");
            }
            $uid=sql_fetch_one_cell("select uid from sys_city where cid=$row[cid]");
            updateUserPrestige($uid);
            
        }      
        
		return $id;

	}
}
 ?>